import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { UserControlApiService } from 'src/app/api/user-control-api.service';

@Component({
  selector: 'app-regist',
  templateUrl: './regist.component.html',
  styleUrls: ['./regist.component.less'],
  providers: [UserControlApiService]
})
export class RegistComponent implements OnInit {

  passwordVisible: boolean;
  userInfoForm: FormGroup
  constructor(
    private fb: FormBuilder,
    private router: Router,
    private loginApi: UserControlApiService,
  ) {
    this.userInfoForm = fb.group({
      userName: [null, [Validators.required]],
      password: [null, [Validators.required]]
    })
  }

  ngOnInit(): void {
  }

  regist() {
    for (const key in this.userInfoForm.controls) {
      this.userInfoForm.controls[key].markAsDirty();
      this.userInfoForm.controls[key].updateValueAndValidity();
    }
    if(this.userInfoForm.valid){

      this.loginApi.regist(this.userInfoForm.value).subscribe(
        respone => {
          console.log(respone);
        }
      )
    }
  }

  returnToLogin(e: MouseEvent) {
    e.preventDefault();
    this.router.navigateByUrl('/login')
  }

}
